package com.xd.spring.liteflow.component;

import cn.hutool.core.util.RandomUtil;
import com.xd.spring.liteflow.entity.UserInfo;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Component("c")
@Slf4j
public class CCmp extends NodeComponent {
    @Override
    public void process() throws Exception {
        String name = Thread.currentThread().getName();
        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
        StackTraceElement stackTraceElement = stackTraceElements[1];
        String className = stackTraceElement.getClassName();
        String methodName = stackTraceElement.getMethodName();
        int lineNumber = stackTraceElement.getLineNumber();
        log.info("{} {} {} {} {} begin", name, className, methodName, lineNumber, System.currentTimeMillis());
        TimeUnit.SECONDS.sleep(3);
        service();
        log.info("{} {} {} {} {} end", name, className, methodName, lineNumber, System.currentTimeMillis());
    }

    private void service() {
        UserInfo userInfo = this.getContextBean(UserInfo.class);
        log.info("{}", ToStringBuilder.reflectionToString(userInfo, ToStringStyle.JSON_STYLE));
        try {
            Assert.notNull(userInfo, "userInfo不能为空");
            Assert.notNull(userInfo.getId(), "userInfo的id不能为空");
            Assert.isNull(userInfo.getAge(), "userInfo的age必须为空");
            userInfo.setAge(RandomUtil.randomInt(20, 60));
        } catch (Exception e) {
            log.info("用户赋值异常", e);
        }
        log.info("{}", ToStringBuilder.reflectionToString(userInfo, ToStringStyle.JSON_STYLE));
    }
}
